package com.industrial.premu.controller.chart;

import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.service.IPreMuBenefitEvaluationService;
import com.industrial.premu.vo.PreMuChartsVo;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/chart")
public class PreMuAreaDimensionController extends BaseController {

    @Autowired
    private IPreMuBenefitEvaluationService preMuBenefitEvaluationService;

    /**
     * 区域维度指标分析（近五年评价情况，亩产效益评价比例）
     */
    @GetMapping(value = "/getDimension")
    public AjaxResult getDimension(PreMuChartsDto preChartsDto) {
        int years=Integer.valueOf(preChartsDto.getYears());

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){

            preChartsDto.setProvince("370000");
        }

        if(years>=2019){
            preChartsDto.setEndYears(String.valueOf(years));
            int  startYears=years-4;
            if(startYears>2019){
                preChartsDto.setStartYears(String.valueOf(startYears));
            }else{
                preChartsDto.setStartYears("2019");
            }
            preChartsDto.setYears("");
        }
        List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.groupDimensionLevel(preChartsDto);
        return  success(chartsVoList);
    }


    @GetMapping(value = "/getDimensionproportion")
    public AjaxResult getDimensionproportion(PreMuChartsDto preChartsDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){

            preChartsDto.setProvince("370000");
        }
        List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.groupProportionLevel(preChartsDto);

        PreMuBenefitEvaluation preMuBenefitEvaluation=new PreMuBenefitEvaluation();
        preMuBenefitEvaluation.setYears(preChartsDto.getYears());
        preMuBenefitEvaluation.setCompanyType(preChartsDto.getCompanyType());
        List<PreMuBenefitEvaluation> preMuBenefitEvaluationList=preMuBenefitEvaluationService.selectPreMuBenefitEvaluationList(preMuBenefitEvaluation);
        BigDecimal  sum=new BigDecimal(preMuBenefitEvaluationList.size());
        for (int i = 0; i < chartsVoList.size(); i++) {
            BigDecimal  levelSum=new BigDecimal(chartsVoList.get(i).getCompanyNum());
            BigDecimal result=levelSum.divide(sum,2,BigDecimal.ROUND_HALF_UP);

            chartsVoList.get(i).setProportionLevel(String.valueOf(result.multiply(new BigDecimal(100))));
        }
        return  success(chartsVoList);
    }

    @GetMapping(value = "/groupArea")
    public AjaxResult groupArea(PreMuChartsDto preChartsDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
        }
        List<PreMuChartsVo> chartsVoList=   preMuBenefitEvaluationService.groupDimensionArea(preChartsDto);
        PreMuBenefitEvaluation preMuBenefitEvaluation=new PreMuBenefitEvaluation();
        preMuBenefitEvaluation.setYears(preChartsDto.getYears());
        preMuBenefitEvaluation.setCompanyType(preChartsDto.getCompanyType());
        preMuBenefitEvaluation.setMjLevel(preChartsDto.getLevelType());
        List<PreMuBenefitEvaluation> preMuBenefitEvaluationList=preMuBenefitEvaluationService.selectPreMuBenefitEvaluationList(preMuBenefitEvaluation);
        BigDecimal  sum=new BigDecimal(preMuBenefitEvaluationList.size());
        for (int i = 0; i < chartsVoList.size(); i++) {
            BigDecimal  companyNum=new BigDecimal(chartsVoList.get(i).getCompanyNum());
            if(sum.compareTo(BigDecimal.valueOf(0))!=0){
                BigDecimal result=companyNum.divide(sum,2,BigDecimal.ROUND_HALF_UP);
                chartsVoList.get(i).setProportionLevel(String.valueOf(result.multiply(new BigDecimal(100))));
            }else{
                chartsVoList.get(i).setProportionLevel("0");
            }
        }
        return  success(chartsVoList);
    }




}
